package com.peng;

public class TankBulletCollider implements Collider {
    @Override
    public boolean collide(GameObject g1, GameObject g2) {
        Tank tank;
        Bullet bullet;
        if (g1 instanceof Tank && g2 instanceof Bullet) {
            tank = (Tank) g1;
            bullet = (Bullet) g2;

        } else if (g1 instanceof Bullet && g2 instanceof Tank) {
            bullet = (Bullet) g1;
            tank = (Tank) g2;
        } else {
            return false;
        }
        if (tank.getGroup() == bullet.getGroup()) {
            return false;
        }

        if (bullet.getRectangle().intersects(tank.getRectangle())) {
            tank.die();
            bullet.die();
            new Explode(tank.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2,
                    tank.getY() + Tank.HEIGHT / 2 - Explode.HEIGHT / 2);
            return true;
        }
        return false;
    }


}
